# from cv2 import cv2
#
import os
import shutil
from PIL import Image
import threading
import threadpool

root_path = 'H:\\迅雷下载\\night\\'
dst_dir = 'H:\\迅雷下载\\cut\\gray\\'


def mymovefile(srcfile, dstpath):
    if not os.path.isfile(srcfile):
        print("%s not exist!" % (srcfile))
    else:
        fpath, fname = os.path.split(srcfile)
        if not os.path.exists(dstpath):
            os.makedirs(dstpath)
        shutil.move(srcfile, dstpath + fname)


def is_color_image(url):
    # 判断灰图函数
    im = Image.open(url)
    pix = im.convert('RGB')
    width = im.size[0]
    height = im.size[1]
    is_gray = True
    for x in range(width):
        for y in range(height):
            r, g, b = pix.getpixel((x, y))
            r = int(r)
            g = int(g)
            b = int(b)
            if (r == g) and (g == b):
                pass
            else:
                is_gray = False
    return is_gray


def is_scale(path):
    # 判断尺寸函数
    if path.find('jpg') != -1:
        im = Image.open(path)
        pix = im.convert('RGB')
        width = im.size[0]
        height = im.size[1]

        scale = 16 / 9
        if width / height == scale:
            return True
        else:
            return False


# dst_path = 'H:/迅雷下载/test2014/cut'
#
# def cut_save(path):
#     file_name = path.split('/')[-1]
#     if path.find('jpg') != -1:
#         im = Image.open(path)
#         width = im.size[0]
#         height = im.size[1]
#
#         if width >= 640 and height >=360:
#             print('正在裁剪:', file_name)
#             new_img = im.crop((0, 0, 640, 360))
#             new_img.save(dst_path + "/" + file_name, quality=95)

# def detect_mv(path):
#     # 这里是完整的路径
#     pass
#     # if is_scale(url):
#     #     print(url, '符合比例')
#     #     mymovefile(url, dstpath=dst_dir)
#     if path.find('jpg') != -1:
#         if is_color_image(path):
#             print('是灰图', path)
#             mymovefile(path, dstpath=dst_dir)



def listFiles(dirPath):
    # 准备一个空列表，用来存储遍历数据
    fileList = []
    fileName = []
    ''' os.walk(dirPath) ：走查指定的文件夹路径
            root  ：代表目录的路径
            dirs  ：一个list，包含了dirpath下所有子目录文件夹的名字
            files ：一个list，包含了所有非目录文件的名字
    '''
    for root, dirs, files in os.walk(dirPath):

        # fileName.append()
        # 循环遍历列表：files【所有文件】，仅得到不包含路径的文件名
        for fileObj in files:
            # 空列表写入遍历的文件名称，兵勇目录路径拼接文件名称
            fileName.append(fileObj)
            path = os.path.join(root, fileObj).replace('\\', '/')
            fileList.append(path)

    # 打印一下列表存储内容：指定文件夹下所有的文件名
    return fileList


all_file_list = listFiles(root_path)
def detect_mv(file_name_and_confidence):
    # 这里文件名，先找到完整路径
    file_name = file_name_and_confidence.split(',')[0]
    confidence = float("%.2f"%(float(file_name_and_confidence.split(',')[1])))

    for full_path in all_file_list:
        # 如果完整路径包含包含文件名
        if full_path.find(file_name) is not -1:
            # print('包含文件名，完整路径为 ', full_path, '自信值为 ', confidence)
            if 0.91 <= confidence < 1:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\10\\')
                print(confidence, '=目录10')
            elif 0.81 <= confidence < 9:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\9\\')
                print(confidence, '=目录9')
            elif 0.71 <= confidence < 8:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\8\\')
                print(confidence, '=目录8')
            elif 0.61 <= confidence < 7:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\7\\')
                print(confidence, '=目录7')
            elif 0.51 <= confidence < 6:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\6\\')
                print(confidence, '=目录6')
            elif 0.41 <= confidence < 5:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\5\\')
                print(confidence, '=目录5')
            elif 0.31 <= confidence < 4:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\4\\')
                print(confidence, '=目录4')
            elif 0.21 <= confidence < 3:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\3\\')
                print(confidence, '=目录3')
            elif 0.11 <= confidence < 2:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\2\\')
                print(confidence, '=目录2')
            elif 0.01 <= confidence < 0.1:
                mymovefile(full_path, dstpath='H:\\迅雷下载\\night\\1\\')
                print(confidence, '=目录1')



txt_file = 'H:\\迅雷下载\\final.txt'
with open(txt_file, 'r') as file:
    txt_data_list = file.readlines()

pool = threadpool.ThreadPool(50)
res = threadpool.makeRequests(detect_mv, txt_data_list)
[pool.putRequest(i) for i in res]
pool.wait()






# for (root, dirs, files) in os.walk(root_path):
#     print(type(files))
# for img_path in files:
#     total_path = root_path + img_path
#     if is_color_image(total_path):
#         print('灰图' + total_path)
#         mymovefile(total_path, dstpath=dst_dir)
#     else:
#         print('彩图')
